package com.tcm.pay.request;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.XmlUtil;
import com.base.core.network.SimpleHttpUtils;
import com.base.core.network.http.ResponseEntity;
import com.base.core.utils.R;
import com.tcm.pay.common.HttpRequest;
import com.tcm.pay.domain.dto.TransferFixedDTO;
import com.tcm.pay.enums.BusinessCodeEnum;
import com.tcm.pay.util.Base64Util;
import org.springframework.stereotype.Component;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

@Component
public class Transfe{

    public Map<String, Object> transferFixed(TransferFixedDTO transferFixedDTO) {

        String url = "https://gwapi.yemadai.com/transfer/transferFixed";

        if (ObjectUtil.isEmpty(transferFixedDTO.getTransId())) {
            R.throwFail(BusinessCodeEnum.BILL_NO_NOT_EXISTS.getCode(), BusinessCodeEnum.BILL_NO_NOT_EXISTS.getMessage());
        }
        transferFixedDTO.setAccountNumber("50829");
        if (ObjectUtil.isEmpty(transferFixedDTO.getNotifyURL())) {
            transferFixedDTO.setNotifyURL("http://api.zetikeji.com/api/pay/transfe/notify");
        }

        if (ObjectUtil.isEmpty(transferFixedDTO.getTt())) {
            transferFixedDTO.setTt("0");
        }
        if (ObjectUtil.isEmpty(transferFixedDTO.getBankCode())) {
            R.throwFail(BusinessCodeEnum.BANK_CODE_NOT_FOUND.getCode(), BusinessCodeEnum.BANK_CODE_NOT_FOUND.getMessage());
        }
        if (ObjectUtil.isEmpty(transferFixedDTO.getProvice())) {
            R.throwFail(BusinessCodeEnum.PROVICE_NOT_FOUND.getCode(), BusinessCodeEnum.PROVICE_NOT_FOUND.getMessage());
        }
        if (ObjectUtil.isEmpty(transferFixedDTO.getCity())) {
            R.throwFail(BusinessCodeEnum.CITY_NOT_FOUND.getCode(), BusinessCodeEnum.CITY_NOT_FOUND.getMessage());
        }
        if (ObjectUtil.isEmpty(transferFixedDTO.getAccountName())) {
            R.throwFail(BusinessCodeEnum.ACCOUNT_NAME_NOT_FOUND.getCode(), BusinessCodeEnum.ACCOUNT_NAME_NOT_FOUND.getMessage());
        }
        if (ObjectUtil.isEmpty(transferFixedDTO.getCardNo())) {
            R.throwFail(BusinessCodeEnum.CARD_NO_NOT_FOUND.getCode(), BusinessCodeEnum.CARD_NO_NOT_FOUND.getMessage());
        }
        if (ObjectUtil.isEmpty(transferFixedDTO.getAmount())) {
            R.throwFail(BusinessCodeEnum.AMOUNT_NOT_FOUND.getCode(), BusinessCodeEnum.AMOUNT_NOT_FOUND.getMessage());
        }

        transferFixedDTO.setSignType("RSA");
        String secureCode = HttpRequest.makeSignForTransferFixed(transferFixedDTO.getTransId(), transferFixedDTO.getAccountNumber(), transferFixedDTO.getCardNo(), transferFixedDTO.getAmount());
        transferFixedDTO.setSecureCode(secureCode);

        StringBuffer xml = new StringBuffer();
        xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
        xml.append("<yemadai>");
        xml.append("<accountNumber>").append(transferFixedDTO.getAccountNumber()).append("</accountNumber>");
        xml.append("<notifyURL>").append(transferFixedDTO.getNotifyURL()).append("</notifyURL>");
        xml.append("<tt>").append(transferFixedDTO.getTt()).append("</tt>");
        xml.append("<signType>").append(transferFixedDTO.getSignType()).append("</signType>");
        xml.append("<transferList>");
        xml.append("<transId>").append(transferFixedDTO.getTransId()).append("</transId>");
        xml.append("<bankCode>").append(transferFixedDTO.getBankCode()).append("</bankCode>");
        xml.append("<provice>").append(transferFixedDTO.getProvice()).append("</provice>");
        xml.append("<city>").append(transferFixedDTO.getCity()).append("</city>");
        xml.append("<branchName>").append(transferFixedDTO.getBranchName()).append("</branchName>");
        xml.append("<accountName>").append(transferFixedDTO.getAccountName()).append("</accountName>");
        xml.append("<idNo>").append(transferFixedDTO.getIdNo()).append("</idNo>");
        xml.append("<phone>").append(transferFixedDTO.getPhone()).append("</phone>");
        xml.append("<cardNo>").append(transferFixedDTO.getCardNo()).append("</cardNo>");
        xml.append("<amount>").append(transferFixedDTO.getAmount()).append("</amount>");
        xml.append("<secureCode>").append(transferFixedDTO.getSecureCode()).append("</secureCode>");
        xml.append("</transferList>");
        xml.append("</yemadai>");

        String xmlData = Base64Util.encode(xml.toString());

        Map<String, String> params = new HashMap<>();
        params.put("transData", xmlData);
        ResponseEntity res = SimpleHttpUtils.post(url, params, false);
        byte[] resData = Base64Util.decode(res.getBody());
        String resXml = new String(resData, StandardCharsets.UTF_8);
        return XmlUtil.xmlToMap(resXml);


    }

}
